import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class LetTheWholeWorldWait {

	Scanner in;

	PrintWriter out;

	static class LocalTime implements Comparable<LocalTime> {

		int hour;

		int min;

		LocalTime(String s) {
			this(Integer.parseInt(s.substring(0, s.indexOf(':'))), Integer.parseInt(s.substring(s.indexOf(":") + 1)));
		}

		private LocalTime(int hh, int mm) {
			this.hour = hh;
			this.min = mm;
			norm();
		}

		private void norm() {
			hour += min / 60;
			min = min % 60;
		}

		void inc() {
			min++;
			norm();
		}

		LocalTime add(int mins) {
			return new LocalTime(hour, min + mins);
		}

		@Override
		public int compareTo(LocalTime second) {
			if (this.hour != second.hour) {
				return this.hour - second.hour;
			}
			return this.min - second.min;
		}

		@Override
		public String toString() {
			return String.format("%02d:%02d", hour, min);
		}
	}

	static class Interval implements Comparable<Interval> {

		LocalTime low;

		LocalTime high;

		Interval(String s1, String s2) {
			this.low = new LocalTime(s1);
			this.high = new LocalTime(s2);
		}

		Interval(LocalTime low, LocalTime high) {
			this.low = low;
			this.high = high;
		}

		boolean isInInterval(LocalTime curr) {
			return low.compareTo(curr) <= 0 && curr.compareTo(high) < 0;
		}

		boolean isFinishedIn(LocalTime curr) {
			return curr.compareTo(high) <= 0;
		}

		int durationMinutes() {
			return (high.hour - low.hour) * 60 + high.min - low.min;
		}

		@Override
		public int compareTo(Interval second) {
			return this.low.compareTo(second.low);
		}

		@Override
		public String toString() {
			return low + " " + high;
		}
	}

	void solve() {
		int nIntervals = in.nextInt();
		Interval[] tempIntervals = new Interval[nIntervals];

		for (int i = 0; i < nIntervals; i++) {
			tempIntervals[i] = new Interval(in.next(), in.next());
		}

		int timeToGet = in.nextInt();

		Interval workday = new Interval("08:00", "18:00");
		LocalTime start = new LocalTime("08:00");
		LocalTime finish = new LocalTime("08:00").add(timeToGet);

		List<Interval> intervals = new ArrayList<>();
		for (Interval i : tempIntervals) {
			if (i.durationMinutes() >= timeToGet) {
				intervals.add(i);
			}
		}
		Collections.sort(intervals);

		Interval widest = null;

		while (workday.isInInterval(start)) {

			for (Interval i : intervals) {
				if (i.isFinishedIn(finish)) {
					LocalTime currStart = start.add(0);
					LocalTime currFinish = i.low.add(timeToGet);
					Interval curr = new Interval(currStart, currFinish);
					if (widest == null || widest.durationMinutes() < curr.durationMinutes()) {
						widest = curr;
					}
					break;
				}
			}

			start.inc();
			finish.inc();
		}
		out.println(widest);
	}

	void run() {
		in = new Scanner(System.in);
		out = new PrintWriter(System.out);
		try {
			solve();
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		new LetTheWholeWorldWait().run();
	}

}
